package com.util;

import io.netty.channel.Channel;

import com.cmd.UserLoginCMD;
import com.player.User;
import com.player.WorldMgr;
import com.qy.util.LogUtil;
import com.qy.util.StaticData;
import com.socket.Command;

/**
 * 命令基类
 */
public abstract class AbstractCommand implements Command {

	@Override
	public void execute(Channel userChannel, PBMessage packet) {
		User user = null;
		try {
			
			if(packet.getCode()==1){
				new UserLoginCMD().execute(userChannel, packet);
			}
			
			int userId = packet.getId();
			if (!WorldMgr.isExist(userId,packet.getType())) {
				LogUtil.warn("code " + packet.getCode() + " not found player " + userId + ",can not continue execute.");
				return;
			}
			user = WorldMgr.getGamePlayer(userId,packet.getType());
			if (user != null) {
				execute(user, packet);
			}
		} catch (Exception e) {
			if (user != null) {
				LogUtil.error("player msg :" +packet.getId() + "code = " + packet.getCode() + ", has exception :", e);
			} else {
				LogUtil.error("code = " + packet.getCode() + ", has exception :", e);
			}
			// TODO 异常抛出测试
			ErrorEvent.HandleErro(user, StaticData.codeType.other_error.getType());
		}
	}

	/**
	 * 子类处理逻辑
	 * @param gamePlayer
	 * @param packet
	 * @throws Exception
	 */
	public abstract void execute(User user, PBMessage packet) throws Exception;
}
